package com.pet.shop.service.product;

import com.github.pagehelper.Page;
import com.pet.exceptions.BusinessException;
import com.pet.shop.dto.PageDto;
import com.pet.shop.entity.product.Product;
import com.pet.shop.entity.product.ProductNorm;
import com.pet.shop.entity.product.ProductProperty;
import com.pet.shop.entity.shop.AdvertInfo;
import com.pet.shop.mappers.product.ProductMapper;
import com.pet.shop.mappers.product.ProductNormMapper;
import com.pet.shop.mappers.product.ProductPropertyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductNormMapper productNormMapper;
    @Autowired
    private ProductPropertyMapper productPropertyMapper;

    @Override
    public int deleteEntity(Long id) {
        return productMapper.delete(id);
    }

    @Override
    public int deleteEntityList(List<Long> idList) {
        if(idList == null || idList.isEmpty()) return 0;
        int i = 0;
        for (Long id : idList) {
            i += productMapper.delete(id);
        }
        return i;
    }

    @Override
    public Product insertEntity(Product record) {
        if(record == null){
            throw new BusinessException("要新增的产品不能为空");
        }
        if(CollectionUtils.isEmpty(record.getNormList()) && record.getPrice() == null){
            throw new BusinessException("价格不能为空");
        }
        record.setViewTimes(0);
        record.setStatus(1);
        productMapper.insert(record);

        if(!CollectionUtils.isEmpty(record.getNormList())){
            record.getNormList().forEach(norm -> {
                norm.setProductId(record.getId());
                norm.setCreateTime(new Date());
                norm.setCreatorId(record.getCreatorId());
                productNormMapper.insert(norm);
            });
        }
        if(!CollectionUtils.isEmpty(record.getPropertyList())){
            record.getPropertyList().forEach(property -> {
                property.setProperty(property.getProperty().replaceAll("；", ";"));
                property.setCreateTime(new Date());
                property.setCreatorId(record.getCreatorId());
                property.setProductId(record.getId());
                productPropertyMapper.insert(property);
            });
        }
        return record;
    }

    @Override
    public Product getEntity(Long id) {
        Product product = productMapper.get(id);
        product.setNormList(productNormMapper.queryByProductId(id));
        product.setPropertyList(productPropertyMapper.queryByProductId(id));
        return product;
    }

    @Override
    public int updateEntity(Product record) {
        Set<Long> normIdList = productNormMapper.queryIdByProductId(record.getId());
        if(!CollectionUtils.isEmpty(record.getNormList())){
            record.getNormList().forEach(norm -> {
                //新增或者删除
                if(norm.getId() == null){
                    norm.setProductId(record.getId());
                    norm.setCreateTime(new Date());
                    norm.setCreatorId(record.getCreatorId());
                    productNormMapper.insert(norm);
                }else{
                    normIdList.remove(norm.getId());
                    norm.setModifyTime(new Date());
                    norm.setModifierId(record.getModifierId());
                    productNormMapper.update(norm);
                }
            });
        }
        Set<Long> propertyIdList = productPropertyMapper.queryIdByProductId(record.getId());
        if(!CollectionUtils.isEmpty(record.getPropertyList())){
            //新增或者删除
            record.getPropertyList().forEach(property -> {
                property.setProperty(property.getProperty().replaceAll("；", ";"));
                if(property.getId() == null){
                    property.setProductId(record.getId());
                    property.setCreateTime(new Date());
                    property.setCreatorId(record.getCreatorId());
                    productPropertyMapper.insert(property);
                }else{
                    propertyIdList.remove(property.getId());
                    property.setModifyTime(new Date());
                    property.setModifierId(record.getModifierId());
                    productPropertyMapper.update(property);
                }
            });
        }
        //删除
        normIdList.forEach(id -> productNormMapper.delete(id));
        propertyIdList.forEach(id -> productPropertyMapper.delete(id));
        return productMapper.update(record);
    }

    @Override
    public PageDto<Product> queryPage(PageDto<Product> pageDto) {
        Page<Product> page = productMapper.queryPage(pageDto);
        pageDto.setPageResult(page);
        if(page.getResult() == null || page.getResult().isEmpty()) return pageDto;
        List<Long> productIds = new ArrayList<>();
        Map<Long, Product> productMap = new HashMap<>();
        page.getResult().forEach(product -> {
            productIds.add(product.getId());
            productMap.put(product.getId(), product);
            if(product.getNormList() == null) product.setNormList(new ArrayList<>());
            if(product.getPropertyList() == null) product.setPropertyList(new ArrayList<>());
        });
        List<ProductProperty> properties = productPropertyMapper.queryByProductIdList(productIds);
        List<ProductNorm> norms = productNormMapper.queryByProductIdList(productIds);
        if(properties != null) properties.forEach(property -> {
            productMap.get(property.getProductId()).getPropertyList().add(property);
        });
        if(norms != null) norms.forEach(norm -> {
            productMap.get(norm.getProductId()).getNormList().add(norm);
        });

        return pageDto;
    }

    @Override
    public List<Product> queryProductInfoList(Map<String,Object> map){
        List<Product> products = productMapper.queryProductList(map);
        if(products == null) products = new ArrayList<>();
        products.forEach(product -> {
            if(product.getViewTimes() == null) product.setViewTimes(0);
        });
        return products;
    }

    @Override
    public List<ProductNorm> queryProductNormList(Long productId){
        return productNormMapper.queryByProductId(productId);
    }

    @Override
    public List<ProductProperty> queryProductPropertyList(Long productId){
        return productPropertyMapper.queryByProductId(productId);
    }

    @Override
    public void updateProductRemark(Product product) {
        productMapper.update(product);
    }
}
